package com.yunkai.plate_lpr;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.ImageView;

import java.util.Random;


public class ImageViewRec extends ImageView implements Runnable {
    private Rect rect;
    private Paint paint;
    private static  ImageViewRec imageViewRec;
    private boolean isscan = false;
    public static ImageViewRec getInstant(){
        return imageViewRec;
    }
    public void stopScan(){
        imageViewRec = null;
        isscan = false;
    }

    public ImageViewRec(Context context, AttributeSet attrs){
        super(context, attrs);
        imageViewRec = this;
        rect = new Rect();
        paint = new Paint();
//        Thread thread = new Thread(this);
//        thread.setPriority(1);
//        thread.start();
    }
    public ImageViewRec(Context context){
        super(context);
        imageViewRec = this;
        rect = new Rect();
        paint = new Paint();

    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //获取控件需要重新绘制的区域
        canvas.getClipBounds(rect);
        rect.top++;
        rect.left++;
        rect.bottom--;
        rect.right--;
        paint.setColor(Color.WHITE);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(8);
        canvas.drawRect(rect, paint);
     /*   int height = rect.bottom - rect.top;
        rect.top+=height*30/100;
        rect.left+=rect.right*16/100;
        rect.bottom-=height*30/100;
        rect.right-=rect.right*16/100;
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(2);
        canvas.drawRect(rect, paint);*/
//        if(isscan){
//            scan(canvas);
//        }


    }

    @Override
    public void run() {
        while(isscan){
            try {
                // Log.i("扫描线程","正在扫描");
                ImageViewRec.this.postInvalidate();
                // handlerR.sendEmptyMessage(1);

                Thread.sleep(50);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        ImageViewRec.this.postInvalidate();
    }



    private int indexX = 0;
    private int indexY = 1;
    private int stepsize = 5;
    public void scan(Canvas canvas){
        Paint paint = new Paint();
        int alpa = 255;
        int lastline = 0;
        for(int i = indexY;i > 0 && alpa >0;i -= 5,alpa -= 20){
            lastline = i;
            paint.setARGB(alpa,60,250,90);
            canvas.drawLine(indexX,i,this.getWidth(),i,paint);
            for(int l = 0;l<this.getWidth(); l+=5){
                canvas.drawLine(l,i,l,i-5,paint);
                Random ra =new Random();
                int nn = ra.nextInt(50);
                if(nn == 1 && alpa > 100){
                    paint.setColor(Color.WHITE);
                    canvas.drawCircle(l+2.5f,i-2.5f,1.5f,paint);
                    paint.setARGB(alpa,60,250,90);
                }

            }
        }
        indexY +=stepsize;
        if(lastline > this.getHeight()){
            indexY =0;
        }

    }

  @Override
  protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    int height = Math.abs(top - bottom);
    stepsize = height/(2*1000/50);
    super.onLayout(changed, left, top, right, bottom);
  }
}
